/*
 * FrmMDIApplication.java
 *
 * Created on __DATE__, __TIME__
 */

package osiris;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.util.Iterator;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.SwingConstants;
import javax.swing.border.BevelBorder;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreeSelectionModel;

public class FrmMDIApplication extends javax.swing.JFrame {

	
	private static final long serialVersionUID = 1L;

	private javax.swing.JDesktopPane desktopPane;
	private JTree tree;
	private javax.swing.JMenuItem aboutMenuItem;
	private javax.swing.JMenuItem contentMenuItem;
	private javax.swing.JMenuItem copyMenuItem;
	private javax.swing.JMenuItem cutMenuItem;
	private javax.swing.JMenuItem deleteMenuItem;
	private javax.swing.JMenu editMenu;
	private javax.swing.JMenuItem exitMenuItem;
	private javax.swing.JMenu fileMenu;
	private javax.swing.JMenu helpMenu;
	private javax.swing.JMenuBar menuBar;
	private javax.swing.JMenuItem openMenuItem;
	private javax.swing.JMenuItem pasteMenuItem;
	private javax.swing.JMenuItem saveAsMenuItem;
	private javax.swing.JMenuItem saveMenuItem;
	
	
	/** Creates new form FrmMDIApplication */
	public FrmMDIApplication() {
		initComponents();
		this.setExtendedState(JFrame.MAXIMIZED_BOTH);
		this.setTitle("Osiris ERP Poject");
	}

	
	private void initComponents() {

		desktopPane = new javax.swing.JDesktopPane();
		menuBar= new javax.swing.JMenuBar();
		fileMenu = new javax.swing.JMenu();
		openMenuItem = new javax.swing.JMenuItem();
		saveMenuItem = new javax.swing.JMenuItem();
		saveAsMenuItem = new javax.swing.JMenuItem();
		exitMenuItem = new javax.swing.JMenuItem();
		editMenu = new javax.swing.JMenu();
		cutMenuItem = new javax.swing.JMenuItem();
		copyMenuItem = new javax.swing.JMenuItem();
		pasteMenuItem = new javax.swing.JMenuItem();
		deleteMenuItem = new javax.swing.JMenuItem();
		helpMenu = new javax.swing.JMenu();
		contentMenuItem = new javax.swing.JMenuItem();
		aboutMenuItem = new javax.swing.JMenuItem();

		setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

		fileMenu.setText("File");
		openMenuItem.setText("Open");
		fileMenu.add(openMenuItem);
		saveMenuItem.setText("Save");
		fileMenu.add(saveMenuItem);
		saveAsMenuItem.setText("Save As ...");
		fileMenu.add(saveAsMenuItem);
		exitMenuItem.setText("Exit");
		exitMenuItem.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				exitMenuItemActionPerformed(evt);
			}
		});

		fileMenu.add(exitMenuItem);
		menuBar.add(fileMenu);
		editMenu.setText("Edit");
		cutMenuItem.setText("Cut");
		editMenu.add(cutMenuItem);
		copyMenuItem.setText("Copy");
		editMenu.add(copyMenuItem);
		pasteMenuItem.setText("Paste");
		editMenu.add(pasteMenuItem);
		deleteMenuItem.setText("Delete");
		editMenu.add(deleteMenuItem);
		menuBar.add(editMenu);
		helpMenu.setText("Help");
		contentMenuItem.setText("Contents");
		helpMenu.add(contentMenuItem);
		aboutMenuItem.setText("About");
		helpMenu.add(aboutMenuItem);
		menuBar.add(helpMenu);
		setJMenuBar(menuBar);
	

		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
		
		getContentPane().setLayout(layout);
		
		layout.setHorizontalGroup(layout.createParallelGroup(
				javax.swing.GroupLayout.Alignment.LEADING).addComponent(
				desktopPane, javax.swing.GroupLayout.DEFAULT_SIZE, 400,
				Short.MAX_VALUE));
		layout.setVerticalGroup(layout.createParallelGroup(
				javax.swing.GroupLayout.Alignment.LEADING).addComponent(
				desktopPane, javax.swing.GroupLayout.DEFAULT_SIZE, 279,
				Short.MAX_VALUE));
		
		desktopPane.setLayout(new BorderLayout());

		JPanel statusPanel = new JPanel();
		statusPanel.setBorder(new BevelBorder(BevelBorder.LOWERED));
		desktopPane.add(statusPanel, BorderLayout.SOUTH);
		statusPanel.setPreferredSize(new Dimension(desktopPane.getWidth()/2, 26));
		statusPanel.setLayout(new BoxLayout(statusPanel, BoxLayout.X_AXIS));
		JLabel statusLabel = new JLabel("status");
		statusLabel.setHorizontalAlignment(SwingConstants.LEFT);
		statusPanel.add(statusLabel);
		desktopPane.add(Box.createRigidArea(new Dimension(5,0)));
		JPanel statusPanel2 = new JPanel();
		statusPanel2.setBorder(new BevelBorder(BevelBorder.LOWERED));
		desktopPane.add(statusPanel2, BorderLayout.NORTH);
		statusPanel2.setPreferredSize(new Dimension(desktopPane.getWidth()/2, 26));
		statusPanel2.setLayout(new BoxLayout(statusPanel2, BoxLayout.X_AXIS));
		JLabel statusLabel2 = new JLabel("status2");
		statusLabel2.setHorizontalAlignment(SwingConstants.LEFT);
		statusPanel2.add(statusLabel2);
		
		
		DefaultMutableTreeNode top =  new DefaultMutableTreeNode("Menu Principal de Osiris ERP");
		createNodes(top, OsirisController.getInstance().getMenu());
		final JTree tree = new JTree(top);
		
		tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);

		//Listen for when the selection changes.
		tree.addTreeSelectionListener(new TreeSelectionListener() {public void valueChanged(TreeSelectionEvent e) {
			//Returns the last path element of the selection.
			//This method is useful only when the selection model allows a single selection.
			    DefaultMutableTreeNode node = (DefaultMutableTreeNode) tree.getLastSelectedPathComponent();

			    if (node == null)
			    //Nothing is selected.     
			    return;

			    Object nodeInfo = node.getUserObject();
			    if (node.isLeaf()) {
			    	TreeNodeAction item = (TreeNodeAction)nodeInfo;
			        System.out.println(item.getAction());
			    } else {
			    	System.out.println("OOPS");; 
			    }}});
		
		JScrollPane treePanel = new JScrollPane(tree);
		desktopPane.add(treePanel, BorderLayout.WEST);
		
		pack();
	}
	
	public static void main(String args[]) {
		java.awt.EventQueue.invokeLater(new Runnable() {
			public void run() {
				new FrmMDIApplication().setVisible(true);
			}
		});
	}
	
	private void exitMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_exitMenuItemActionPerformed
		System.exit(0);
	}
	
	private void createNodes(DefaultMutableTreeNode top, MenuItem aMenu){
		 DefaultMutableTreeNode category = null;
		 DefaultMutableTreeNode item = null;
		
		Iterator<MenuItem> menu=aMenu.getChilds().iterator();
		
		while (menu.hasNext()){
			MenuItem aMenuItem = menu.next();
			if(aMenuItem.getChilds()==null){
				item=new DefaultMutableTreeNode(new TreeNodeAction(aMenuItem.getText(),aMenuItem.getText()));
			
				top.add(item);
			}else{
				category=new DefaultMutableTreeNode(aMenuItem.getText());
				top.add(category);
				this.createNodes(category, aMenuItem);
			}
		}
	}

	private void createNodes(DefaultMutableTreeNode top) {
	    DefaultMutableTreeNode category = null;
	    DefaultMutableTreeNode book = null;
	    
	    category = new DefaultMutableTreeNode("Books for Java Programmers");
	    top.add(category);
	    
	    //original Tutorial
	    book = new DefaultMutableTreeNode("The Java Tutorial: A Short Course on the Basics");
	    category.add(book);
	    
	    //Tutorial Continued
	    book = new DefaultMutableTreeNode("The Java Tutorial Continued: The Rest of the JDK");
	    category.add(book);
	    
	    //Swing Tutorial
	    book = new DefaultMutableTreeNode("The Swing Tutorial: A Guide to Constructing GUIs");
	    category.add(book);

	    //...add more books for programmers...

	    category = new DefaultMutableTreeNode("Books for Java Implementers");
	    top.add(category);

	    //VM
	    book = new DefaultMutableTreeNode("The Java Virtual Machine Specification");
	    category.add(book);

	    //Language Spec
	    book = new DefaultMutableTreeNode("The Java Language Specification");
	    category.add(book);
	}


}